//
//  UIButton+MMAdd.m
//  EraPig
//
//  Created by Mac_Mini on 2021/3/29.
//

#import "UIButton+MMAdd.h"
#import <objc/runtime.h>

static const char *key = "UIButton_Action_Key";

@implementation UIButton (MMAdd)

- (void)addTarget:(id)target forControlEvents:(UIControlEvents)controlEvents block:(void (^)(void))block{
    objc_setAssociatedObject(self, key, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:target action:@selector(action111) forControlEvents:controlEvents];
}
- (void)action111{
    void(^block)(void) = objc_getAssociatedObject(self, key);
    if (block) {
        block();
    }
}

- (void)verticalImageAndTitle:(CGFloat)spacing {
    CGSize imageSize = self.imageView.frame.size;
    CGSize titleSize = self.titleLabel.frame.size;
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.titleLabel.font, NSFontAttributeName, nil];
    CGSize textSize = [self.titleLabel.text sizeWithAttributes:dic];
    CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
    if ((titleSize.width + 0.5) < frameSize.width) {
        titleSize.width = frameSize.width;
    }
    CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);
    self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);
    self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageSize.width, - (totalHeight - titleSize.height), 0);
}

- (void)layoutButtonEdge:(ButtonLayoutStyle)buttonLayoutStyle spacing:(CGFloat)spacing{
    [self.titleLabel sizeToFit];
    [self.imageView sizeToFit];
    CGSize imageSize = self.imageView.frame.size;
    CGSize titleSize = self.titleLabel.frame.size;
    
    CGFloat spec = spacing*0.5;//图片与文字各自承担一半的间距
    /**
     图文混排的偏移
     ButtonLayoutStyleImageInTop:
     
     ButtonLayoutStyleImageInLeft:
     第一步：image 左侧减少一段距离，右侧增加一段距离；
     第二步：title 左侧增加一段距离，右侧减少一段距离；
     
     ButtonLayoutStyleImageInRight:
     第一步：把image移到右面，相当于上侧和下侧不变，左侧增加一段距离，右侧减少一段距离；
     第二步：把title移到左面，相当于上侧和下侧不变，左侧减少一段距离，右侧增加一段距离；
     */
    switch (buttonLayoutStyle) {
        case ButtonLayoutStyleImageInTop:{
//            self.imageEdgeInsets = UIEdgeInsetsMake(-imageSize.height-titleSize.height-spec, 0, 0, -titleSize.width);
//            self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, -imageSize.height-titleSize.height+spec, 0);
        }
            break;
        case ButtonLayoutStyleImageInLeft:{
            switch (self.contentHorizontalAlignment) {
                case UIControlContentHorizontalAlignmentLeft:{
                    self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
                    self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);
                }
                    break;
                    
                default:{
                    self.imageEdgeInsets = UIEdgeInsetsMake(0, -spec, 0, spec);
                    self.titleEdgeInsets = UIEdgeInsetsMake(0, spec, 0, -spec);
                }
                    break;
            }
        }
            break;
        case ButtonLayoutStyleImageInBottom:{
            
        }
            break;
        case ButtonLayoutStyleImageInRight:{
            switch (self.contentHorizontalAlignment) {
                case UIControlContentHorizontalAlignmentRight:{
                    self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -titleSize.width);
                    self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, imageSize.width+spacing);
                }
                    break;
                    
                default:{
                    self.imageEdgeInsets = UIEdgeInsetsMake(0, titleSize.width+spec, 0, -titleSize.width-spec);
                    self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width-spec, 0, imageSize.width+spec);
                }
                    break;
            }
        }
            break;
        case ButtonLayoutStyleOverlap:{
            self.imageEdgeInsets = UIEdgeInsetsMake(0, titleSize.width*0.5, 0, -titleSize.width*0.5);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width*0.5, 0, imageSize.width*0.5);
        }
            break;
        default:
            break;
    }
}

@end
